﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PPI.Entities.Interface;
using PPI.Entities.Kml;

namespace PPI.Entities.Inspections
{
    public class EInspectionResult : IGoogleEarth
    {
        #region Private

        private Int32 m_id;
        private Int32 m_SeverId;

        private EInspectionTypes m_InspectionType;
        private EResultType m_Resultype;
        private string m_LastInspection;
        private string m_Comments;
        private List<ENote> m_Notes;
        private bool m_isElectrical;
        private EInspection m_Inspection;

        #endregion

        #region Properties
        public int SeverId
        {
            get { return m_SeverId; }
            set { m_SeverId = value; }
        }


        public int Id
        {
            get { return m_id; }
            set { m_id = value; }
        }

        public EInspectionTypes InspectionType
        {
            get { return m_InspectionType; }
            set { m_InspectionType = value; }
        }

        public EResultType Resultype
        {
            get { return m_Resultype; }
            set { m_Resultype = value; }
        }

        public string LastInspection
        {
            get { return m_LastInspection; }
            set { m_LastInspection = value; }
        }

        public string Comments
        {
            get { return m_Comments; }
            set { m_Comments = value; }
        }

        public List<ENote> Notes
        {
            get { return m_Notes; }
            set { m_Notes = value; }
        }

        public bool IsElectrical
        {
            get { return m_isElectrical; }
            set { m_isElectrical = value; }
        }

        public EInspection Inspection
        {
            get { return m_Inspection; }
            set { m_Inspection = value; }
        }

        #endregion

        public EInspectionResult()
        {
            m_id = Int32.MinValue;
            m_Comments = "";
            m_InspectionType = new EInspectionTypes();
            m_LastInspection = "";
            m_Notes = new List<ENote>();
            m_Resultype = new EResultType();
        }

        public EKmlFile ToKml()
        {
            EKmlFile _file = new EKmlFile();
            _file.KMLCode = GetPlaceMark();
            return _file;
        }
        protected virtual string GetPlaceMark()
        {
            StringBuilder s = new StringBuilder();
            s.AppendLine("<Placemark id=\"" + GetPlacemarkId() + "\">");
            s.AppendLine(" <Style>");
            s.AppendLine("     <BalloonStyle>");
            s.AppendLine("         <color>F9DDCD</color>");
            s.AppendLine("         <text>$[description]</text>");
            s.AppendLine("     </BalloonStyle>");
            s.AppendLine(" <IconStyle>");
            s.AppendLine("     <Icon>");
            s.AppendLine("         <href>" + GetIcon() + "</href>");
            s.AppendLine("     </Icon>");
            s.AppendLine(" </IconStyle>");
            s.AppendLine("</Style>");
            s.AppendLine(" <name>" + GetHtmlName() + "</name>");
            s.AppendLine(" <description>");
            s.AppendLine("     <![CDATA[" + GetHtmlPlacemark() + "]]>");
            s.AppendLine(" </description>");
            s.AppendLine(GetPositionKML());
            s.AppendLine("</Placemark>");
            return s.ToString();
        }

        private string GetHtmlPlacemark()
        {
            StringBuilder _Shtml = new StringBuilder();
            _Shtml.AppendLine("<table style='font-family:Verdana; font-size:10px;' border='0'>");
            //Header
            _Shtml.AppendLine(" <tr>");
            _Shtml.AppendLine("     <td colspan='2'><b>Inspection Result | Inspection [" + Inspection.Id + "]</b></td>");
            _Shtml.AppendLine(" </tr>");
            _Shtml.AppendLine(" <tr>");
            _Shtml.AppendLine("     <td>");
            _Shtml.AppendLine(GenerateHtmlImages());
            _Shtml.AppendLine("     </td>");
            _Shtml.AppendLine("     <td>");
            _Shtml.AppendLine("         <table>");
            _Shtml.AppendLine("             <tr>");
            _Shtml.AppendLine("                 <td>Result Type: " + m_Resultype.Name + "</td>");
            _Shtml.AppendLine("                 <td>Inspection Type: " + m_InspectionType.Name + "</td>");
            _Shtml.AppendLine("             </tr>");
            _Shtml.AppendLine("             <tr>");
            _Shtml.AppendLine("                 <td><b>Notes<b></td>");
            _Shtml.AppendLine("             </tr>");
            _Shtml.AppendLine("             <tr>");
            _Shtml.AppendLine("                 <td><ul>");
            foreach (ENote note in m_Notes)
            {
                _Shtml.AppendLine("<li>" + note.Name + "</li>");
            }
            _Shtml.AppendLine("                 </ul></td>");
            _Shtml.AppendLine("             </tr>");
            _Shtml.AppendLine("             <tr>");
            _Shtml.AppendLine("                 <td>Company:</td>");
            _Shtml.AppendLine("                 <td>" + Inspection.Company.Name + "</td>");
            _Shtml.AppendLine("             </tr>");

            _Shtml.AppendLine("             <tr>");
            _Shtml.AppendLine("                 <td>Inspector:</td>");
            _Shtml.AppendLine("                 <td>" + Inspection.Inspector.Name + "</td>");
            _Shtml.AppendLine("             </tr>");

            _Shtml.AppendLine("             <tr>");
            _Shtml.AppendLine("                 <td>Length:</td>");
            if (Inspection.Length != Int32.MinValue)
                _Shtml.AppendLine("                 <td>" + Inspection.Length + "</td>");
            else
                _Shtml.AppendLine("                 <td></td>");
            _Shtml.AppendLine("             </tr>");


            _Shtml.AppendLine("             <tr>");
            _Shtml.AppendLine("                 <td>Specie:</td>");
            _Shtml.AppendLine("                 <td>" + Inspection.Specie.Name + "</td>");
            _Shtml.AppendLine("             </tr>");

            _Shtml.AppendLine("             <tr>");
            _Shtml.AppendLine("                 <td>Top:</td>");
            _Shtml.AppendLine("                 <td>" + Inspection.TopType.Name + "</td>");
            _Shtml.AppendLine("             </tr>");

            _Shtml.AppendLine("             <tr>");
            _Shtml.AppendLine("                 <td>Class:</td>");
            _Shtml.AppendLine("                 <td>" + Inspection.Class.Name + "</td>");
            _Shtml.AppendLine("             </tr>");

            _Shtml.AppendLine("             <tr>");
            _Shtml.AppendLine("                 <td>Treatment:</td>");
            _Shtml.AppendLine("                 <td>" + Inspection.Treatment.Name + "</td>");
            _Shtml.AppendLine("             </tr>");

            _Shtml.AppendLine("             <tr>");
            _Shtml.AppendLine("                 <td>Acquisition Date:</td>");
            _Shtml.AppendLine("                 <td>" + Inspection.AcquisitionDate.ToString() + "</td>");
            _Shtml.AppendLine("             </tr>");

            _Shtml.AppendLine("             <tr>");
            _Shtml.AppendLine("                 <td>Number</td>");
            _Shtml.AppendLine("                 <td>" + Inspection.Number + "</td>");
            _Shtml.AppendLine("             </tr>");

            _Shtml.AppendLine("             <tr>");
            _Shtml.AppendLine("                 <td>Last Visit:</td>");
            _Shtml.AppendLine("                 <td>" + Inspection.LastVisit + "</td>");
            _Shtml.AppendLine("             </tr>");
            _Shtml.AppendLine("             <tr>");
            _Shtml.AppendLine("                 <td>Year:</td>");
            _Shtml.AppendLine("                 <td>" + Inspection.Year + "</td>");
            _Shtml.AppendLine("             </tr>");

            _Shtml.AppendLine("             <tr>");
            _Shtml.AppendLine("                 <td colspan='2'>Comments : " + m_Comments + "</td>");
            _Shtml.AppendLine("             </tr>");
            _Shtml.AppendLine("         </table>");
            _Shtml.AppendLine("     </td>");
            _Shtml.AppendLine(" </tr>");
            _Shtml.AppendLine("</table>");
            return _Shtml.ToString();
        }

        private string GetPositionKML()
        {
            StringBuilder s = new StringBuilder();
            s.AppendLine("<Point id=\"test1\">");
            s.AppendLine("  <coordinates>" + Inspection.Longitude.ToString().Replace(",", ".") + "," + Inspection.Latitude.ToString().Replace(",", ".") + " ,10000</coordinates>");
            s.AppendLine("</Point>");
            return s.ToString();
        }

        private string GetHtmlName()
        {
            return "Inspection Result " + Id.ToString();
        }

        private string GetIcon()
        {
            string returnIcon = "";
            if(IsElectrical)
            {
                returnIcon = PPI.Constants.K_INSPECTIONELECPROBICON;
            }else
            {
                Int32 value = EResultType.ParseValue(m_Resultype.Name);
                switch(value)
                {
                    case 1:
                        returnIcon = PPI.Constants.K_INSPECTIONRESULT1;
                        break;
                    case 2:
                        returnIcon = PPI.Constants.K_INSPECTIONRESULT2;
                        break;
                    case 30:
                    case 31:
                    case 32:
                    case 33:
                        returnIcon = PPI.Constants.K_INSPECTIONRESULT3;
                        break;
                }
            }
            return returnIcon;
        }

        private string GetPlacemarkId()
        {
            return "Result_" + Id.ToString();
        }

        public bool IsEmpty()
        {
            throw new NotImplementedException();
        }

        private string GenerateHtmlImages()
        {
            StringBuilder _temp = new StringBuilder();
            _temp.AppendLine("         <table border='0'>");
            foreach (EInspectionImage i in Inspection.Images)
            {
                _temp.AppendLine("         <tr>");
                _temp.AppendLine(i.GenerateHTML());
                _temp.AppendLine("         </tr>");
            }
            _temp.AppendLine("         </table>");
            return _temp.ToString();
        }

        public string GetNotes()
        {
            StringBuilder notesBuilder = new StringBuilder();
            foreach (ENote note in m_Notes)
            {
                notesBuilder.AppendLine(note.Name + ",");
            }
            return notesBuilder.ToString().Length > 0 ? notesBuilder.ToString().Substring(0, notesBuilder.ToString().Length - 1) : "";
        }
    }
}
